package com.isoftstone.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.isoftstone.pojo.Notice;
import com.isoftstone.service.NoticeService;
import com.isoftstone.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    //分页查询
    @GetMapping("/notice")
    public Result<?> getAllNotices(@RequestParam(defaultValue = "1") Integer currentPage,
                                   @RequestParam(defaultValue = "3") Integer pageSize,
                                    String title){
        IPage page = noticeService.queryAllNotices(currentPage, pageSize, title);
        return Result.success(page);
    }
    //新增通知
    @PostMapping("/notice")
    public Result<?> insertNotice(@RequestBody Notice notice){
        return noticeService.insertNotice(notice) ? Result.success("新增成功") : Result.error("新增失败");
    }

    //更新通知
    @PutMapping("/notice")
    public Result<?> updateNotice(@RequestBody Notice notice){
        return noticeService.updateNotice(notice) ? Result.success("更新成功") : Result.error("更新失败");
    }
    //删除通知
    @DeleteMapping("/notice/{id}")
    public Result<?> deleteNotice(@PathVariable Integer id){
        return noticeService.deleteNotice(id) ? Result.success("删除成功") : Result.error("删除失败");
    }
    //查询指定id的通知
    @GetMapping("/notice/{id}")
    public Result<?> getNoticeById(@PathVariable Integer id){
        return Result.success(noticeService.getById(id));
    }
}
